package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override//分类列表
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //1.分页查询
        Page<Dish> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        page = dishMapper.selectPage(page, wrapper);
        //2.赋值菜系,口味
        List<Dish> list = page.getRecords();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Dish dish : list) {
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
                //赋值口味
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return page;
    }

    @Override//新增菜品
    @CacheEvict(value = "dish",allEntries = true)
    public void save(Dish dish) {
        //1.添加菜品到dish
        dishMapper.insert(dish);
        //2.添加口味到flavor
        List<DishFlavor> flavors = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override//修改菜品，回显菜品
    public Dish findById(Long id) {
        Dish dish = dishMapper.selectById(id);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavors);
        return dish;
    }

    @Override//修改菜品
    @CacheEvict(value = "dish",allEntries = true)
    public void update(Dish dish) {
        dishMapper.updateById(dish);
        List<DishFlavor> flavors = dish.getFlavors();
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override//批量删除
    @CacheEvict(value = "dish",allEntries = true)
    public void delete(List<Long> ids) {
        //判断菜品状态，启售无法删除
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        Integer count = dishMapper.selectCount(wrapper);
        if (count>0) {
            throw new CustomException("删除的菜品必须为停售");
        }
        dishMapper.deleteBatchIds(ids);
        //删除口味
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(wrapper1);
    }

    @Override//批量启售、禁售
    @CacheEvict(value = "dish",allEntries = true)
    public void updateStatus(Integer status,List<Long> ids) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        Dish dish=new Dish();
        dish.setStatus(status);
        dishMapper.update(dish,wrapper);
    }

    @Override//新增套餐菜品分类选择
    public List<Dish> findByCategoryId(Dish dish) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.like(StrUtil.isNotEmpty(dish.getName()),Dish::getName,dish.getName());
        wrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishMapper.selectList(wrapper);
        return list;
    }
    //h5菜品列表
    @Override
    @Cacheable(value = "dish",key = "#categoryId",unless ="#result==null||#result.size()==0")
    public List<Dish> findDishList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId).eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dishFlavorLambdaQueryWrapper);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }
}
